//
//  ByTransitionController.m
//  beibei_ios
//
//  Created by pro on 16/4/21.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByTransitionController.h"

@implementation ByTransitionController

/** 动画时间 */
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    return 0.5f;
}

/** 动画效果 */
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    CGRect initialFrame = [transitionContext initialFrameForViewController:fromVC];
    CGRect finialFrame = [transitionContext finalFrameForViewController:toVC];
    
    UIView *mask = [[UIView alloc] init]; // 黑色半透明视图
    mask.backgroundColor = [UIColor blackColor];
    [containerView addSubview:mask];
    [containerView addSubview:toVC.view];
    
    // push
    if (!self.reverse) {
        
        [containerView sendSubviewToBack:fromVC.view];
        [containerView bringSubviewToFront:toVC.view];
        
        fromVC.view.frame = initialFrame;
        mask.frame = fromVC.view.frame;
        mask.alpha = 0.0;
        toVC.view.frame = finialFrame;
        toVC.view.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH, 0);
        
        /** 5.27 添加 层次效果 */
//        fromVC.view.layer.transform = CATransform3DIdentity;
//        CATransform3D t = CATransform3DIdentity;
//        t.m34 = - 1 / 2000;
//        t = CATransform3DTranslate(t, 0, 0, -200);
//        t = CATransform3DScale(t, 0.95, 0.95, 1);

        
        [UIView animateWithDuration:[self transitionDuration:transitionContext]
                         animations:^{
                             
                             /** 5.27 添加 层次效果 */
//                             fromVC.view.layer.transform = t;
                             
                             mask.alpha = 0.4;
                             toVC.view.transform = CGAffineTransformIdentity;
                             toVC.view.layer.transform = CATransform3DIdentity;
                             fromVC.view.frame = CGRectOffset(initialFrame, -SCREEN_WIDTH / 4, 0);
                             mask.frame = fromVC.view.frame;

                         }
                         completion:^(BOOL finished) {
                             
                             [mask removeFromSuperview];
                             
                             [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                             
                         }];

    }
    else {
        
        [containerView sendSubviewToBack:toVC.view];
        [containerView bringSubviewToFront:fromVC.view];

        mask.alpha = 0.4;
        toVC.view.frame = CGRectOffset(finialFrame, -SCREEN_WIDTH / 4, 0);;
        mask.frame = toVC.view.frame;
        
        /** 5.27 添加 层次效果 */
//        CATransform3D t = CATransform3DIdentity;
//        t.m34 = - 1 / 2000;
//        t = CATransform3DTranslate(t, 0, 0, -200);
//        t = CATransform3DScale(t, 0.95, 0.95, 1);
//        toVC.view.layer.transform = t;
        
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext]
                         animations:^{
                             /** 5.27 添加 层次效果 */
//                             toVC.view.layer.transform = CATransform3DIdentity;
                             
                             mask.alpha = 0.0;
                             toVC.view.frame = finialFrame;
                             mask.frame = toVC.view.frame;
                             fromVC.view.transform = CGAffineTransformMakeTranslation(SCREEN_WIDTH, 0);
                         }
                         completion:^(BOOL finished) {
                             
                             [mask removeFromSuperview];
                             
                             [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                             
                         }];
    }
    

    
}



@end
